package template;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controler.LogoutListener;

import facade.ClientSystem;
import facade.DeliverySystem;
import facade.LoginSystem;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public abstract class AbstractSystem extends JFrame{
	
	private static final long serialVersionUID = 1L;
	protected Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
	public static JFrame CURRENTSYSTEM;
	public static String systemType;
	
	public class CloseSystemListener extends WindowAdapter{
		
		@Override
		public void windowClosed(WindowEvent evt){
			if(CURRENTSYSTEM.getClass().getSimpleName().equals("LoginSystem")){
				if(systemType.equals("client")){
					new ClientSystem();
				}else if(systemType.equals("livreur")){
					new DeliverySystem();
				}else if(systemType.equals("restaurateur")){
					
				}
			}else{
				new LoginSystem();
			}
		}
		
	}
	
	public class ExitProgramListener implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent evt){
			System.exit(0);
		}
	}
	
	public void setCurrentSystem(JFrame fm){
		CURRENTSYSTEM = fm;
	}
	
	public static void setSystemType(String str){
		systemType = str;
	}
	
	protected JMenuBar createRootMainMenu(String str){
		JMenuBar mainMenu = new JMenuBar();
		
		JMenu menuSystem = new JMenu(str);
		JMenuItem logoutMenuItem = new JMenuItem("Logout");
		logoutMenuItem.addActionListener(new LogoutListener());
		menuSystem.add(logoutMenuItem);
		JMenuItem exitMenuItem = new JMenuItem("Exit");
		exitMenuItem.addActionListener(new ExitProgramListener());
		menuSystem.add(exitMenuItem);
		
		mainMenu.add(menuSystem);
		
		return mainMenu;
	}
}
